﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Secured_ViewAddExpenseHead : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (conn.State != ConnectionState.Open)
        {
            conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
            try
            {
                conn.Open();
            }
            catch (SqlException ex)
            {
                ltMsg.Text = ex.Message;
            }
        }
        txtAddHead.Focus();
        if (!Page.IsPostBack)
            FillLV();
    }

    private void FillLV()
    {
        cmd = new SqlCommand("select HeadID, ExpenseHead from HAT_ExpenseHeads", conn);
        da = new SqlDataAdapter(cmd.CommandText, conn);
        ds = new DataSet();
        try
        {
            da.Fill(ds, "NewFilteredTable");
            grdExpenseHeads.DataSource = ds.Tables["NewFilteredTable"].DefaultView;
            grdExpenseHeads.DataBind();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    protected void grdExpenseHeads_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdExpenseHeads.EditIndex = -1;
        FillLV();
    }
    protected void grdExpenseHeads_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdExpenseHeads.EditIndex = e.NewEditIndex;
        FillLV();
    }
    protected void btnAddHead_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("Insert into HAT_ExpenseHeads(ExpenseHead) Values(@ExpenseHead)", conn);
        cmd.Parameters.Add("@ExpenseHead", SqlDbType.VarChar).Value = txtAddHead.Text.Trim();
        try
        {
            cmd.ExecuteNonQuery();
            txtAddHead.Text = string.Empty;
            FillLV();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
}
